<section class="content-header">
    <h1>
        <?php echo $this->_lang['resource_management']; ?>
    </h1>
    <ol class="breadcrumb">
        <li><a href="./"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><i class="fa fa-gears"></i> <?php echo $this->_lang['resource_management']; ?></li>
    </ol>
</section>

<!-- Main content -->
<section class="content">
    <div class="nav-tabs-custom">
        <!-- Tabs within a box -->
        <ul class="nav nav-tabs" id="resources">
            <?php if ($this->checkUserRole('new_template', $_SESSION['internalKey']) || $this->checkUserRole('edit_template', $_SESSION['internalKey'])) { ?>
            <li class="active"><a href="#templates" data-toggle="tab"><?php echo $this->_lang['manage_templates']; ?></a></li>
            <?php }?>
            <?php if ($this->checkUserRole('new_snippet', $_SESSION['internalKey']) || $this->checkUserRole('edit_snippet', $_SESSION['internalKey'])) { ?>
            <li><a href="#snippets" data-toggle="tab"><?php echo $this->_lang['manage_snippets']; ?></a></li>
            <?php } ?>
            <?php if ($this->checkUserRole('new_chunk', $_SESSION['internalKey']) || $this->checkUserRole('edit_chunk', $_SESSION['internalKey'])) { ?>
            <li><a href="#chunks" data-toggle="tab"><?php echo $this->_lang['manage_htmlsnippets']; ?></a></li>
            <?php } ?>
            <li><a href="#templateVars" data-toggle="tab"><?php echo $this->_lang['template_var_tab']; ?></a></li>
            <li><a href="#categories" data-toggle="tab"><?php echo $this->_lang['manage_categories']; ?></a></li>
        </ul>
        <div class="tab-content">
        	<?php if ($this->checkUserRole('new_template', $_SESSION['internalKey']) || $this->checkUserRole('edit_template', $_SESSION['internalKey'])) { ?>
            <div class="tab-pane active" id="templates" style="position: relative;">
            	<?php if ($this->checkUserRole('new_template', $_SESSION['internalKey'])) { ?>
                <div style="padding:5px 0;margin-bottom:10px;"><a href="javascript:;" id="newTemplate" class="btn btn-primary"><?php echo $this->_lang['new_template']; ?></a></div>
                <?php } ?>
                <p class="menuHeader"><?php echo $this->_lang['template_management_msg']; ?></p>
                <table class="display" id="template-tbl">
                  <thead>
                    <tr>
                        <th>Template ID</th>
                        <th>Template Name</th>
                        <th>Description</th>
                        <th>Locked</th>
                    </tr>
                  </thead>
                  <tbody>
                <?php
                    $sql = "select templatename, id, description, locked from ".$this->db."site_templates order by templatename";
                    $rs = $this->dbQuery($sql);
                    $limit = $this->recordCount($rs);
                        for($i=0; $i<$limit; $i++) {
                            $row = $this->fetchRow($rs);
                            $id = strval($row['id']);
                ?>
                    <tr>
                        <td><?php echo $row['id'];?></td>
                        <td><a id="<?php echo $row['id'];?>" class="templateBtn"><?php echo $row['templatename']; ?></a></td>
                        <td><?php echo $row['description']!='' ? $row['description'] : '' ; ?></td>
                        <td><?php echo $row['locked']==1 ? ' <i><small>('.$this->_lang['template_locked_message'].')</small></i>' : "" ; ?></td>
                    </tr>
                <?php
                        }// end for
                ?>
                    </tbody>
                  </table>
                <script type="text/javascript">
                    <?php if ($this->checkUserRole('new_template', $_SESSION['internalKey'])) { ?>
                    $('#newTemplate').click(function() {
                        Etomite.editResource('template');
                    });
                    <?php } ?>
                    <?php if ($this->checkUserRole('edit_template', $_SESSION['internalKey'])) { ?>
                    $('.templateBtn').click(function() {
                        Etomite.editResource('template', $(this).attr('id'));
                    });
                    <?php } ?>
                    tTable = $('#template-tbl').dataTable({
                            "bJQueryUI": true,
                            "sPaginationType": "full_numbers"
                        });
                </script>
            </div>
            <?php } ?>
            <?php if ($this->checkUserRole('new_snippet', $_SESSION['internalKey']) || $this->checkUserRole('edit_snippet', $_SESSION['internalKey'])) { ?>
            <div class="tab-pane" id="snippets" style="position: relative;">
            	<?php if ($this->checkUserRole('new_snippet', $_SESSION['internalKey'])) { ?>
                <div style="padding:5px 0;margin-bottom:10px;"><a href="javascript:;" id="newSnippet" class="btn btn-primary"><?php echo $this->_lang['new_snippet']; ?></a></div>
                <?php } ?>
                <p><?php echo $this->_lang['snippet_management_msg']; ?></p>
                
                <?php
                    // modified 2010-04-22 by John Carlson to add section function and organizaion of snippets
                    $sqlS = "select * from ".$this->db."site_section order by sort_order DESC,name ASC";
                    $rsS = $this->dbQuery($sqlS);
                    $limitS = $this->recordCount($rsS);
                    if($limitS<1){
                        echo $_lang['no_results'];
                    }
					$stabs = array();
					$stabcontent = array();
                    for($z=0;$z<$limitS;$z++){
                        $rowS = $this->fetchRow($rsS);
						$sql = "select name, id, description, locked from ".$this->db."site_snippets where section=".$rowS['id']." order by name";
						$rs = $this->dbQuery($sql);
						$limit = $this->recordCount($rs);
						if ($limit > 0) {
							$stabs[] = $rowS['name']; //." - ".$rowS['description'];
							$stc = "<ul>";
							  for($i=0; $i<$limit; $i++) {
								$row = $this->fetchRow($rs);
								$stc .= '<li><a id="'.$row['id'].'" class="snippetBtn">'.$row['name'].'</a>';
								$stc .= $row['description']!='' ? ' - '.$row['description'] : '';
								$stc .= $row['locked']==1 ? ' <i><small>('.$_lang['snippet_locked_message'].')</small></i>' : "";
								$stc .= "</li>";
								
								} // end for for snippets
							 $stc .= "</ul>";
							 $stabcontent[] = $stc;
							 $stc = '';
						}
					} // end for
				?>
                
                <div class="nav-tabs-custom">
                    <!-- Snippet Tabs within a box -->
                    <ul class="nav nav-tabs">
                        <?php 
						$ucSel = '';
						if (count($stabs) > 0) {
							$c = 0;
							foreach($stabs as $tab) {
							?>
                            <li <?php echo ($c=='0') ? 'class="active"':'';?>><a href="#tab<?php echo $c;?>" data-toggle="tab"><?php echo $tab; ?></a></li>
                            <?php	
							$c++;
							}
						} else { $ucSel = 'active'; }?>
                        <li class="<?php echo $ucSel;?>"><a href="#uncategorized" data-toggle="tab"><?php echo $this->_lang['un_categorized']; ?></a></li>
                    </ul>
                    <div class="tab-content">
                    	<?php if (count($stabcontent) > 0) {
							$c = 0;
							foreach($stabcontent as $tabc) {
								?>
                        <div class="tab-pane <?php echo ($c=='0') ? 'active':'';?>" id="tab<?php echo $c;?>" style="position: relative;">
                        	<?php echo $tabc; ?>
                        </div>
                        <?php
							$c++;
							}
						}?>
                        <div class="tab-pane <?php echo $ucSel;?>" id="uncategorized" style="position: relative;">
                        	<?php
							$sql = "select name, id, description, locked from ".$this->db."site_snippets where section<1 order by name";
							  $rs = $this->dbQuery($sql);
							  $limit = $this->recordCount($rs);
							  if($limit<1){
								echo $_lang['no_results'];
							  }
							  echo "<ul>";
							  for($i=0; $i<$limit; $i++) {
								$row = $this->fetchRow($rs);
							?>
							
								<li><a id="<?php echo $row['id'];?>" class="snippetBtn">
									<?php echo $row['name']; ?>
								  </a>
								  <?php echo $row['description']!='' ? ' - '.$row['description'] : '' ; ?>
								  <?php echo $row['locked']==1 ? ' <i><small>('.$_lang['snippet_locked_message'].')</small></i>' : "" ; ?>
								</li>
					
							  <?php
						
								} // end for for snippets
								echo "</ul>";
							  ?>
                        </div>
                    </div>
                </div>
                <script type="text/javascript">
                    <?php if ($this->checkUserRole('new_snippet', $_SESSION['internalKey'])) { ?>
                    $('#newSnippet').click(function() {
                        Etomite.editResource('snippet');
                    });
                    <?php } ?>
                    <?php if ($this->checkUserRole('edit_snippet', $_SESSION['internalKey'])) { ?>
                    $('.snippetBtn').click(function() {
                        Etomite.editResource('snippet', $(this).attr('id'));
                    });
                    <?php } ?>
                </script>
            </div>
            <?php } ?>
            <?php if ($this->checkUserRole('new_chunk', $_SESSION['internalKey']) || $this->checkUserRole('edit_chunk', $_SESSION['internalKey'])) { ?>
            <div class="tab-pane" id="chunks" style="position: relative;">
            	<?php if ($this->checkUserRole('new_chunk', $_SESSION['internalKey'])) { ?>
                <div style="padding:5px 0;margin-bottom:10px;"><a href="javascript:;" id="newChunk" class="btn btn-primary"><?php echo $this->_lang["new_htmlsnippet"]; ?></a></div>
                <?php } ?>
                <p><?php echo $this->_lang['htmlsnippet_management_msg']; ?></p>
                <?php
                    $sqlS = "select * from ".$this->db."site_section order by sort_order DESC,name ASC";
                    $rsS = $this->dbQuery($sqlS);
                    $limitS = $this->recordCount($rsS);
                    if($limitS<1){
                        echo $_lang['no_results'];
                    }
					$stabs = array();
					$stabcontent = array();
                    for($z=0;$z<$limitS;$z++){
                        $rowS = $this->fetchRow($rsS);
						$sql = "select name, id, description, locked from ".$this->db."site_htmlsnippets where section=".$rowS['id']." order by name";
						$rs = $this->dbQuery($sql);
						$limit = $this->recordCount($rs);
						if ($limit > 0) {
							$stabs[] = $rowS['name']; //." - ".$rowS['description'];
							$stc = "<ul>";
							  for($i=0; $i<$limit; $i++) {
								$row = $this->fetchRow($rs);
								$stc .= '<li><a id="'.$row['id'].'" class="chunkBtn">'.$row['name'].'</a>';
								$stc .= $row['description']!='' ? ' - '.$row['description'] : '';
								$stc .= $row['locked']==1 ? ' <i><small>('.$_lang['snippet_locked_message'].')</small></i>' : "";
								$stc .= "</li>";
								
								} // end for for chunks
							 $stc .= "</ul>";
							 $stabcontent[] = $stc;
							 $stc = '';
						}
					} // end for
				?>
                
                <div class="nav-tabs-custom">
                    <!-- Chunks Tabs within a box -->
                    <ul class="nav nav-tabs">
                        <?php 
						$ucSel = '';
						if (count($stabs) > 0) {
							$c = 0;
							foreach($stabs as $tab) {
							?>
                            <li <?php echo ($c=='0') ? 'class="active"':'';?>><a href="#ctab<?php echo $c;?>" data-toggle="tab"><?php echo $tab; ?></a></li>
                            <?php	
							$c++;
							}
						} else { $ucSel = 'active'; }?>
                        <li class="<?php echo $ucSel;?>"><a href="#cuncategorized" data-toggle="tab"><?php echo $this->_lang['un_categorized']; ?></a></li>
                    </ul>
                    <div class="tab-content">
                    	<?php if (count($stabcontent) > 0) {
							$c = 0;
							foreach($stabcontent as $tabc) {
								?>
                        <div class="tab-pane <?php echo ($c=='0') ? 'active':'';?>" id="ctab<?php echo $c;?>" style="position: relative;">
                        	<?php echo $tabc; ?>
                        </div>
                        <?php
							$c++;
							}
						}?>
                        <div class="tab-pane <?php echo $ucSel;?>" id="cuncategorized" style="position: relative;">
                        	<?php
							$sql = "select name, id, description, locked from ".$this->db."site_htmlsnippets where section<1 order by name";
							  $rs = $this->dbQuery($sql);
							  $limit = $this->recordCount($rs);
							  if($limit<1){
								echo $_lang['no_results'];
							  }
							  echo "<ul>";
							  for($i=0; $i<$limit; $i++) {
								$row = $this->fetchRow($rs);
							?>
							
								<li><a id="<?php echo $row['id'];?>" class="chunkBtn">
									<?php echo $row['name']; ?>
								  </a>
								  <?php echo $row['description']!='' ? ' - '.$row['description'] : '' ; ?>
								  <?php echo $row['locked']==1 ? ' <i><small>('.$_lang['snippet_locked_message'].')</small></i>' : "" ; ?>
								</li>
					
							  <?php
						
								} // end for for snippets
								echo "</ul>";
							  ?>
                        </div>
                    </div>
                </div>
                <script type="text/javascript">
                    <?php if ($this->checkUserRole('new_chunk', $_SESSION['internalKey'])) { ?>
                    $('#newChunk').click(function() {
                        Etomite.editResource('chunk');
                    });
                    <?php } ?>
                    <?php if ($this->checkUserRole('edit_chunk', $_SESSION['internalKey'])) { ?>
                    $('.chunkBtn').click(function() {
                        Etomite.editResource('chunk', $(this).attr('id'));
                    });
                    <?php } ?>
                </script>
            </div>
            <?php } ?>
            <?php if ($this->checkUserRole('new_tv', $_SESSION['internalKey']) || $this->checkUserRole('edit_tv', $_SESSION['internalKey'])) { ?>
            <div class="tab-pane" id="templateVars" style="position: relative;">
            	<div style="padding:5px 0;margin-bottom:10px;"><a href="javascript:;" id="newTv" class="btn btn-primary"><?php echo $this->_lang['create_template_var'];?></a></div>
                <p class="menuHeader"><?php echo $this->_lang['template_var_tab_msg'];?></p>
                <table width="100%" cellpadding="0" cellspacing="0" border="0" class="display" id="tv-tbl">
                  <thead>
                    <tr>
                        <th>TV ID</th>
                        <th>TV Name</th>
                        <td>TV Field Name</td>
                        <th>Description</th>
                    </tr>
                  </thead>
                  <tbody>
                <?php
                    $tvs = $this->getIntTableRows($fields="tv_id,field_name,name,description",$from="template_variables");
                    if (count($tvs) > 0) {
                    ?>
                            <?php foreach($tvs as $tv) {?>
                                <tr>
                                    <td><?php echo $tv['tv_id'];?></td>
                                    <td><a id="<?php echo $tv['tv_id'];?>" class="tvBtn"><?php echo $tv['name'];?></a></td>
                                    <td><?php echo $tv['field_name']; ?></td>
                                    <td><?php echo $tv['description']; ?></td>
                                </tr>
                            <?php }?>
                    <?php
                    } else {
                        echo "<tr><td><p><em>".$this->_lang['no_template_vars']."</em></p></td><td></td><td></td><td></td></tr>";
                    }
                ?>
                    </tbody>
                  </table>
                <script type="text/javascript">
                    <?php if ($this->checkUserRole('new_chunk', $_SESSION['internalKey'])) { ?>
                    $('#newTv').click(function() {
                        Etomite.editTV();
                    });
                    <?php } ?>
                    <?php if ($this->checkUserRole('edit_chunk', $_SESSION['internalKey'])) { ?>
                    $('.tvBtn').click(function() {
                        Etomite.editTV($(this).attr('id'));
                    });
                    <?php } ?>
                    tvTable = $('#tv-tbl').dataTable({
                            "bJQueryUI": true,
                            "sPaginationType": "full_numbers"
                        });
                </script>
            </div>
            <?php } ?>
            <div class="tab-pane" id="categories" style="position: relative;">
            	<div style="padding:5px 0;margin-bottom:10px;"><a href="javascript:;" id="newCategory" class="btn btn-primary"><?php echo $this->_lang["create_category"]; ?></a></div>
                <p class="menuHeader"><?php echo $this->_lang['category_management_msg']; ?></p>
                <table width="100%" cellpadding="0" cellspacing="0" border="0" class="display" id="category-tbl">
                  <thead>
                    <tr>
                        <th>Category ID</th>
                        <th>Category Name</th>
                        <th>Description</th>
                    </tr>
                  </thead>
                  <tbody>
                <?php
                    $sql = "select name, id, description from ".$this->db."site_section order by name ASC";
                    $rs = $this->dbQuery($sql);
                    $limit = $this->recordCount($rs);
                        for($i=0; $i<$limit; $i++) {
                            $row = $this->fetchRow($rs);
                            $id = strval($row['id']);
                ?>
                    <tr>
                        <td><?php echo $row['id'];?></td>
                        <td><a id="<?php echo $row['id'];?>" class="categoryBtn"><?php echo $row['name']; ?></a></td>
                        <td><?php echo $row['description']!='' ? $row['description'] : '' ; ?></td>
                    </tr>
                <?php
                        }// end for
                ?>
                </tbody>
                </table>
                <script type="text/javascript">
                    $('#newCategory').click(function() {
                        Etomite.createSection();
                    });
                    /*$('.categoryBtn').click(function() {
                        Etomite.editResource('category', $(this).attr('id'));
                    });*/
                    cTable = $('#category-tbl').dataTable({
                            "bJQueryUI": true,
                            "sPaginationType": "full_numbers"
                        });
                </script>
            </div>
        </div>
    </div>
</section>